﻿using System;
using NUnit.Framework;
using SharpTestsEx;

namespace AltNetHispano.Vale.Tests
{
    public class RegexValidatorTest
    {
        private const string ZipCodePattern = @"^(\d{5}-\d{4}|\d{5}|\d{9})$|^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$";
        private const string EmailRegExPattern = @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";

        [Test]
        public void WhenValidateValueThatMatchesRegExThenReturnEmptyMessage()
        {
            const string anyValidZipCode = "08544";
            CommonValidators.RegExMatch(null, "anyProperty", ZipCodePattern, anyValidZipCode).Should().Be.Empty();
        }

        [Test]
        public void WhenValidateValueThatDontMatchesRegExThenReturnEmptyMessage()
        {
            const string anyInvalidZipCode = "08544dsds";
            CommonValidators.RegExMatch(null, "anyProperty", ZipCodePattern, anyInvalidZipCode).Should().Not.Be.Empty();
        }

        [Test]
        public void WhenValidateWithNullOrEmpyRegExPatternThenThrow()
        {
            IValidator validator = new Validator();
            validator.Executing(v => v.RegExMatch("property", null, null)).Throws<ArgumentNullException>();
            validator.Executing(v => v.RegExMatch("property", string.Empty, null)).Throws<ArgumentNullException>();
        }

        [Test]
        public void WhenValidateNullOrEmpyValueThenReturnEmptyMessage()
        {
            CommonValidators.RegExMatch(null, "anyProperty", ZipCodePattern, string.Empty).Should().Be.Empty();
        }



        //Validate Mail
        [TestCase("valid@email.com")]
        [TestCase("val+id@email.com")]
        [TestCase("val-id@email.com")]
        [TestCase("val=id@email.com")]
        [TestCase("val_id@email.com")]
        [TestCase("val!id@email.com")]
        [TestCase("valid.valid@email.com")]
        public void WhenValidateEMailThatMatchesWithExpectedRegexThenReturnEmptyMessage(string email)
        {
            CommonValidators.RegExMatch(null, "propertyMail", EmailRegExPattern, email).Should().Be.Empty();
        }

        [TestCase("invalid@@email.com")]
        [TestCase("inv@lid@email.com")]
        [TestCase("inva lid@email.com")]
        [TestCase("invalid @ email.com")]
        [TestCase("invalid@ email.com")]
        [TestCase(@"invalid\@email.com")]
        [TestCase("invalid(@email.com")]
        [TestCase("invalid.email.com")]
        public void WhenValidateEMailThatDontMatchesWithExpectedRegexThenReturnEmptyMessage(string email)
        {
            CommonValidators.RegExMatch(null, "propertyMail", EmailRegExPattern, email).Should().Not.Be.Empty();
        }

        //Validate Zip Code
        [TestCase("08540")]
        [TestCase("08544")]
        [TestCase("19103")]
        public void WhenValidateZipCodeThatMatchesWithExpectedRegexThenReturnEmptyMessage(string zipCode)
        {
            CommonValidators.RegExMatch(null, "propertyZipCode", ZipCodePattern, zipCode).Should().Be.Empty();
        }

        [TestCase("08540343")]
        [TestCase("08ds54e4")]
        [TestCase("19 103")]
        [TestCase("q1910_3")]
        [TestCase("1910_3")]
        public void WhenValidateZipCodeThatDontMatchesWithExpectedRegexThenReturnEmptyMessage(string zipCode)
        {
            CommonValidators.RegExMatch(null, "propertyZipCode", ZipCodePattern, zipCode).Should().Not.Be.Empty();
        }
    }
}
